package com.jymf.login.controller;

import com.jymf.common.HttpJsonObject;
import com.jymf.common.MD5;
import com.jymf.common.SessionUtil;
import com.jymf.login.service.LoginService;
import com.jymf.model.Users;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zyj
 * @create 2017-05-23
 **/
@Controller
@RequestMapping("/main")
public class MainController extends HttpJsonObject {

    @Autowired
    private LoginService loginService;

    /**
     * 获取结构
     * @author zyj
     * @date 2017/05/23
     * @param
     * @return
     **/
    @RequestMapping(value = "/getMainResource")
    public void getMainResource(HttpServletResponse response, HttpServletRequest request) throws Exception{
        String parentId = request.getParameter("parentId");
        if(parentId==null||"".equals(parentId)){
            parentId = "1";
        }
        List<Map<String, Object>> childrenId = new ArrayList<Map<String, Object>>();
        Users users = (Users) request.getSession().getAttribute("userInfo");
        if(users!=null){
            childrenId = loginService.getChildrenResource(parentId, users.getId());
        }


        this.outJsonArray(childrenId,response);
    }

    /**
     * 获取资源列表
     * @param response
     * @param request
     */
    @RequestMapping("/getResource")
    public void getResource(HttpServletResponse response, HttpServletRequest request) throws Exception{
        String parentId = request.getParameter("parentId");

        List<Map<String, Object>> childrenId = new ArrayList<Map<String, Object>>();
        Users users = (Users) request.getSession().getAttribute("userInfo");
        if(users!=null){
            childrenId = loginService.getResource(parentId, users.getId());
        }


        this.outJsonArray(childrenId,response);
    }

    /***
     * 获取按钮
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping(value = "/getResBtn",method = RequestMethod.POST)
    public void getResBtn(HttpServletResponse response, HttpServletRequest request) throws Exception{
        String parentId = request.getParameter("parentId");

        List<Map<String, Object>> childrenId = new ArrayList<Map<String, Object>>();
        Users users = (Users) request.getSession().getAttribute("userInfo");
        if(users!=null){
            childrenId = loginService.getResBtn(parentId, users.getId());
        }


        this.outJsonArray(childrenId,response);
    }

    /**
     * 修改密码
     * @param response
     * @param request
     */
    @RequestMapping(value = "/editPasswd", method = RequestMethod.POST)
    public  void editPasswd(HttpServletResponse response, HttpServletRequest request) throws Exception{
        boolean b = false;
        String message = "";
        String passwdNew = request.getParameter("passwdNew");
        String passwdOld = request.getParameter("passwdOld");

        Users users = (Users) request.getSession().getAttribute("userInfo");
        if(passwdNew==null||passwdOld==null||"".equals(passwdNew)||"".equals(passwdOld)||!MD5.EncoderPwdByMd5(passwdOld).equals(users.getLgpasswd())){
            message = "原始密码错误";
        }else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("lgPasswd", MD5.EncoderPwdByMd5(passwdNew));
            map.put("id", users.getId());
            b = loginService.editPasswd(map);
        }

        //修改成功更新session
        if(b){
            users.setLgpasswd(MD5.EncoderPwdByMd5(passwdNew));
            SessionUtil.setSession(request, users);
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("value", b);
        jsonObj.put("message", b);
        this.outJson(jsonObj, response);
    }
}
